<?php

namespace app\user\controller;
use app\user\model\Order;
use app\user\model\Userdata;
use think\Controller;
use think\Db;

/**
 * 用户操作类
 */
class UserAdmin extends Controller {
	public function _initialize() {

		$grade = Userdata::where('id', cookie('uid'))->value('grade');

		$this->assign('grade', $grade);
	}
	/**
	 * 用户订单
	 * @return [type] [description]
	 */
	public function order() {
		if (session('uid')) {
			$order = Order::where('user_id', session('uid'))->paginate(4);
		}else{
			$order = Order::where('user_id', cookie('uid'))->paginate(4);
		}
		
		
		$this->assign('order', $order);

		return view();
	}
	/**
	 * 用户积分查询
	 * @return [type] [description]
	 */
	public function grade() {
		$user = Userdata::get(['id' => cookie('uid')]);
		$order = Order::where('user_id', cookie('uid'))->select();

		$this->assign('user', $user);
		$this->assign('order', $order);
		return view();
	}
	/**
	 * 帮助中心
	 * @return [type] [description]
	 */
	public function help() {
		return view();
	}

	/**
	 * 用户信息
	 * @return [type] [description]
	 */
	public function info() {
		if (session('user')) {
			$user = Userdata::get(['username' => session('user')]);
		}else{
			$user = Userdata::get(['username' => cookie('user')]);
		}
		

		$this->assign('user', $user);
		return view();
	}
	/**
	 * 用户密码修改
	 * @return [type] [description]
	 */
	public function pwd() {
		// var_dump(input('get.'));
		$email = input('get.email');
		$redis = new \think\cache\driver\Redis();
		$email = $redis -> get(input('get.email'));
		// var_dump($email);
		$this->assign('email',$email);
		return view();
	}
	/**
	 * 优惠券查询
	 * @return [type] [description]
	 */
	public function coupon() {
		return view();
	}


	public function change_pwd()
	{
		$email = input('post.email');
		$password = input('post.password');
		$re = input('post.repassword');
		// DB::listen(function($sql){echo $sql;});
		$new = Userdata::where(['email'=>$email])->find();
		$new -> password = md5($password);
		$result = $new -> save();
		if ($result) {
			echo json_encode(['status'=>200]);
		}else{
			echo json_encode(['status'=>403]);
		}
	}
}
